<?php

namespace App\Http\Controllers\Coopen;

use App\Category;
use App\Post;
use App\Setting;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PostController extends Controller
{
    //
    private $postManager;
    private $settingManager;
    private $categoryManager;
    private $request;
    public function __construct (Request $request)
    {
        $this->postManager = new Post();
        $this->settingManager = new Setting();
        $this->categoryManager = new Category();
        $this->request = $request;
    }

    /**
     *文章列表
     */
    public function index($slug){

            $category = $this->categoryManager->where('slug',$slug)->first();
        if(!$category){
            abort(404);
        }else{
            $category = $category->toArray();
        }
        return view('coopen.posts.list.index',
            [
                'slug'=>$slug,
                'request'=>$this->request,
                'category'=>$category]
        );
    }
    public function ajaxList(){
        $id = $this->request->category_id;
            $category = $this->categoryManager->where('id',$id)->first();
        if(!$category){
            abort(404);
        }else{
            $category = $category->toArray();
        }

        $this->request->category_id = $category['id'];
        $this->request->status = Post::PUBLISHED;
        $request = $this->request;
        if($request->page!=1){
            $request->page = $request->page-1;
            $list0 = $this->postManager->getPageList($request,true);
        }else{
            $list0['data'] = [];
        }
        $list =  $this->postManager->getPageList($this->request,true);
        $this->request->limit = $this->request->limit+1;
        $list1 =  $this->postManager->getPageList($this->request,true);

        if($list['data']){
        return view('coopen.posts.list-components.index',[
            'list' =>  $list
        ]);

        } else{
            return '';
        }
    }

    public function detail($id){
        $post = $this->postManager->getPost($id);
        $next = 0;
        $prev = 0;
        if($this->request->next){
            $next = $this->request->next;
        }
        if($this->request->prev){
            $prev = $this->request->prev;
        }
            return view('coopen.posts.view.index',[
                'post'  =>  $post,

//            'next'  =>  $this->postManager->getNext($id,$post->category_id),
//            'prev'  =>  $this->postManager->getPrev($id,$post->category_id)
            ]);

    }

    public function getNext($category_id,$id){
        $model = Post::with(['category']);
        $ids = Category::where('pid',$category_id)->pluck('id');
        if($ids){
            $ids = $ids->toArray();
            array_push($ids,$this->request->category_id);
            $model = $model->whereIn('category_id',$ids);
        }else{
            $model=$model->where('category_id','=',$category_id);
        }
        $model = $model->where('updated_at','>',$id);
    }
}
